package com.yunfei.api.v2;

import com.yunfei.api.v1.magerIndcInfo.service.MagerIndcInfoService;
import com.yunfei.api.v2.contants.QueryHeTongSignInfoContant;
import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.*;
import com.yunfei.api.v2.result.ContractStsResult;
import com.yunfei.api.v2.result.ElecHeTongNoResult;
import com.yunfei.api.v2.result.GetAccountInfo;
import com.yunfei.api.v2.result.addAccountInfo.AddAccountInfoResultModel;
import com.yunfei.api.v2.result.createPersonInfo.CreatePersonInfoJsonRootBean;
import com.yunfei.api.v2.result.tunRegister.TurnRegisterResult;
import com.yunfei.api.v2.service.*;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.mapper.crm.TunRegisterMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 填写收益账号信息
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/v2/accountInfo")
@Api(value = QueryHeTongSignInfoContant.ACCOUNTINFO_NAMESPACE,tags =  QueryHeTongSignInfoContant.ACCOUNTINFO_NAMESPACE )
public class AccountInfoApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private Environment env;
    @Autowired
    private MagerIndcInfoMapper magerIndcInfoMapper;
    @Autowired
    private MagerIndcInfoTemplateMapper magerIndcInfoTemplateMapper;
    @Autowired
    private MagerIndcInfoService magerIndcInfoService;
    @Autowired
    private HTSGetStatusService htsGetStatusService;
    @Autowired
    private HTSGetAccountInfoService htsGetAccountInfoService;
    @Autowired
    private HTSAddAccountService htsAddAccountService;
    @Autowired
    private HTYuYueInfoService htYuYueInfoService;
    @Autowired
    private GetElecHeTongNoService getElecHeTongNoService;
    @Autowired
    private TunRegisterMapper tunRegisterMapper;
    @Autowired
    private CreatePersonInfoService createPersonInfoService;
    @Autowired
    private ElecContCustAddInfoMapper elecContCustAddInfoMapper;
    @Autowired
    private QueryUserNoService queryUserNoService;
    /**
     * @Description: 填写账户信息（投资者）
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/add")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.ADD_ACCOUNT_INFO,notes = QueryHeTongSignInfoContant.ADD_ACCOUNT_INFO,response = AddAccountInfoResultModel.class)
    @ResponseBody
    public Object addAccount(AddAccountParam param,CustNoParam custNoParam){
        //填写完预约信息，需要调用创建个人客户接口
        ResultModel resultModel=null;
        try{
            resultModel=htsAddAccountService.add(param);
            String custNo=custNoParam.getcCustNo();
            if(StringUtil.isNullOrEmpty(custNo)){
                resultModel.setCode(GetAccountInfoEnum.CUSTNO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.CUSTNO_NOTNULL.getMessage());
                return resultModel;
            }
            //验证客户对应的证件号是否已认证
            Map<String,Object> isRenZhengQueryMap=new HashMap<>();
            isRenZhengQueryMap.put("custNo",custNo);
            int isRenZheng=elecContCustAddInfoMapper.isRenZheng(isRenZhengQueryMap);
            //如果已认证，无需调用接口 （上述SQL>=1）
            //如果未认证，调用 创建个人用户 API接口，为客户进行认证。(上述SQL = 0)
            String signerId="";//签署者id，用户id
            TurnRegisterResult turnRegisterResult=tunRegisterMapper.get(isRenZhengQueryMap);
            if(isRenZheng==0){
                if(!StringUtil.isNullOrEmpty(turnRegisterResult)){
                    CreatePersonInfoParam createPersonInfoParam=new CreatePersonInfoParam();
                    createPersonInfoParam.setCaType("B2");//证书类型
                    createPersonInfoParam.setCertifyNum(turnRegisterResult.getIdentityNo());//身份证号码
                    createPersonInfoParam.setCertifyType(turnRegisterResult.getIdentityType());//证件类型中文
                    createPersonInfoParam.setIdentityRegion(turnRegisterResult.getCustAdd());//身份地区
                    createPersonInfoParam.setPhoneNo(turnRegisterResult.getMobileNo());//手机号码
                    createPersonInfoParam.setPhoneRegion(turnRegisterResult.getMobileAdd());//手机号地区
                    createPersonInfoParam.setUserName(turnRegisterResult.getCustName());//企业名称
                    CreatePersonInfoJsonRootBean createPersonInfoJsonRootBean=createPersonInfoService.createPersonInfo(createPersonInfoParam,"");
                    if(createPersonInfoJsonRootBean.getCode()==200){
                        //插入表ELEC_CONT_CUST_ADD_INFO
                        AddElecContCustAddInfoParam addElecContCustAddInfoParam=new AddElecContCustAddInfoParam();
                        addElecContCustAddInfoParam.setCaCustId(signerId);
                        addElecContCustAddInfoParam.setCertNo(turnRegisterResult.getIdentityNo());//证件号码
                        addElecContCustAddInfoParam.setCertTyp(turnRegisterResult.getIdentityType());//证件类型
                        addElecContCustAddInfoParam.setCustAdd(turnRegisterResult.getCustAdd());//身份地区
                        addElecContCustAddInfoParam.setCustId(custNo);//客户号
                        addElecContCustAddInfoParam.setCustNm(turnRegisterResult.getCustName());//客户名称
                        addElecContCustAddInfoParam.setCustType(turnRegisterResult.getCustType());//客户类型
                        addElecContCustAddInfoParam.setMobileAdd(turnRegisterResult.getMobileAdd());//手机号地区
                        addElecContCustAddInfoParam.setMobileNo(turnRegisterResult.getMobileNo());//手机号码
                        elecContCustAddInfoMapper.add(addElecContCustAddInfoParam);
                    }else{
                        return createPersonInfoJsonRootBean;
                    }
                }
            }else{
                //已认证，无需调用创建个人用户的接口，需要插入ELEC_CONT_CUST_ADD_INFO
                //根据证件号码查询签署者id
                String certifyNum=turnRegisterResult.getIdentityNo();
                signerId=queryUserNoService.queryUserNo(certifyNum,"");
            }
            //返回签署者签署者 ID
            AddAccountInfoResultModel addAccountInfoResultModel=new AddAccountInfoResultModel();
            addAccountInfoResultModel.setSignerId(signerId);
            resultModel.setData(addAccountInfoResultModel);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 编辑账户信息（投资者）
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/update")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.UPDATE_ACCOUNT_INFO,notes = QueryHeTongSignInfoContant.UPDATE_ACCOUNT_INFO,response = AddAccountInfoResultModel.class)
    @ResponseBody
    public Object update(UpdateAccountParam param,CustNoParam custNoParam){
        //更新账号信息
        ResultModel resultModel=htsAddAccountService.update(param);
        Map<String,Object> isRenZhengQueryMap=new HashMap<>();
        String custNo=custNoParam.getcCustNo();
        if(StringUtil.isNullOrEmpty(custNo)){
            resultModel.setCode(GetAccountInfoEnum.CUSTNO_NOTNULL.getCode());
            resultModel.setMessage(GetAccountInfoEnum.CUSTNO_NOTNULL.getMessage());
            return resultModel;
        }
        isRenZhengQueryMap.put("custNo",custNo);
        String signerId="";//签署者id，用户id
        TurnRegisterResult turnRegisterResult=tunRegisterMapper.get(isRenZhengQueryMap);
        //根据证件号码查询签署者id
        String certifyNum=turnRegisterResult.getIdentityNo();
        signerId=queryUserNoService.queryUserNo(certifyNum,"");
       if(StringUtil.isNullOrEmpty(signerId)){
           CreatePersonInfoParam createPersonInfoParam=new CreatePersonInfoParam();
           createPersonInfoParam.setCaType("B2");//证书类型
           createPersonInfoParam.setCertifyNum(turnRegisterResult.getIdentityNo());//身份证号码
           createPersonInfoParam.setCertifyType(turnRegisterResult.getIdentityType());//证件类型中文
           createPersonInfoParam.setIdentityRegion(turnRegisterResult.getCustAdd());//身份地区
           createPersonInfoParam.setPhoneNo(turnRegisterResult.getMobileNo());//手机号码
           createPersonInfoParam.setPhoneRegion(turnRegisterResult.getMobileAdd());//手机号地区
           createPersonInfoParam.setUserName(turnRegisterResult.getCustName());//企业名称
           CreatePersonInfoJsonRootBean createPersonInfoJsonRootBean=createPersonInfoService.createPersonInfo(createPersonInfoParam,"");
           if(createPersonInfoJsonRootBean.getCode()==200){
               signerId=createPersonInfoJsonRootBean.getData().getSignerId();
               //插入表ELEC_CONT_CUST_ADD_INFO
               AddElecContCustAddInfoParam addElecContCustAddInfoParam=new AddElecContCustAddInfoParam();
               addElecContCustAddInfoParam.setCaCustId(signerId);
               addElecContCustAddInfoParam.setCertNo(turnRegisterResult.getIdentityNo());//证件号码
               addElecContCustAddInfoParam.setCertTyp(turnRegisterResult.getIdentityType());//证件类型
               addElecContCustAddInfoParam.setCustAdd(turnRegisterResult.getCustAdd());//身份地区
               addElecContCustAddInfoParam.setCustId(custNo);//客户号
               addElecContCustAddInfoParam.setCustNm(turnRegisterResult.getCustName());//客户名称
               addElecContCustAddInfoParam.setCustType(turnRegisterResult.getCustType());//客户类型
               addElecContCustAddInfoParam.setMobileAdd(turnRegisterResult.getMobileAdd());//手机号地区
               addElecContCustAddInfoParam.setMobileNo(turnRegisterResult.getMobileNo());//手机号码
               elecContCustAddInfoMapper.add(addElecContCustAddInfoParam);
           }else{
               return createPersonInfoJsonRootBean;
           }
       }
        //返回签署者签署者 ID
        AddAccountInfoResultModel addAccountInfoResultModel=new AddAccountInfoResultModel();
        addAccountInfoResultModel.setSignerId(signerId);
        resultModel.setData(addAccountInfoResultModel);
        return resultModel;
    }

    /**
     * @Description: 是否可以编辑
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/isEdite")
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.IS_UPDATE,notes = QueryHeTongSignInfoContant.IS_UPDATE,response = ContractStsResult.class)
    @ResponseBody
    public Object isEdite(ReserveNoParam param){
        return htsGetAccountInfoService.isEdite(param);
    }

    /**
     * @Description: 查询受益账号信息
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/get")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_INFO,notes = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_INFO_NOTES,response = GetAccountInfo.class)
    @ResponseBody
    public Object getShouYiZhangHaoInfo(CustNoParam param,ReserveNoParam yueYuParam){
        return htsGetAccountInfoService.getAccountInfo(param,yueYuParam);
    }
    /**
     * @Description: 受益账号选择
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/select")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_SELECT,notes = QueryHeTongSignInfoContant.GET_SHOUYI_ZHANGHAO_SELECT,response = GetAccountInfo.class)
    @ResponseBody
    public Object getShouYiZhangHaoSelect(CustNoParam param){
        return htsGetAccountInfoService.getAccountSelect(param);
    }

    /**
     * @Description: 根据银行卡号自动获取银行信息
     * @return: java.lang.Object
     * @Author: zhengyunfei
     * @Date: 2019/8/12
     * @time:10:31
     */
    @PostMapping(value="/getBankByNo")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_BANK,notes = QueryHeTongSignInfoContant.GET_BANK_NOTES,response = GetAccountInfo.class)
    @ResponseBody
    public Object getBankByNo(BankNoParam param){
        return htsGetAccountInfoService.getBankByNo(param);
    }

    /**
     * @Description: 根据银行卡号自动获取银行信息
     * @return: java.lang.Object
     * @Author: zhengyunfei
     * @Date: 2019/8/12
     * @time:10:31
     */
    @PostMapping(value="/getBankList")
    @ApiOperationSort(value = 2)
    @ApiOperation(position = 2,value = QueryHeTongSignInfoContant.GET_BANK_LIST,notes = QueryHeTongSignInfoContant.GET_BANK_LIST_NOTES,response = GetAccountInfo.class)
    @ResponseBody
    public Object getBankList(){
        return htsGetAccountInfoService.getBankList();
    }
}
